<?php

namespace app\admin\model;

use think\Model;


class Clinic extends Model
{

    

    

    // 表名
    protected $name = 'clinic';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'week_text',
        'time_text',
        'opentime_text'
    ];
    

    


    public function getWeekTextAttr($value, $data)
    {
        $value = $value ?: ($data['week'] ?? '');
        if (strpos($value, ',') !== false) {
            $weeks = explode(',', $value);
            $weekNames = ['1' => '周一', '2' => '周二', '3' => '周三', '4' => '周四', '5' => '周五', '6' => '周六', '7' => '周日'];
            $result = [];
            foreach ($weeks as $week) {
                if (isset($weekNames[$week])) {
                    $result[] = $weekNames[$week];
                }
            }
            return implode(',', $result);
        }
        return $value;
    }

    public function getTimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['time'] ?? '');
        // 如果是逗号分隔的字符串，返回原值；否则按时间戳处理
        if (strpos($value, ',') !== false) {
            return $value;
        }
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getOpentimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['opentime'] ?? '');
        // 开放时间描述是文本字段，直接返回
        return $value;
    }

    protected function setTimeAttr($value)
    {
        // 如果是数组（多选），转换为逗号分隔的字符串
        if (is_array($value)) {
            return implode(',', $value);
        }
        // 如果是逗号分隔的字符串，直接返回
        if (strpos($value, ',') !== false) {
            return $value;
        }
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setOpentimeAttr($value)
    {
        // 开放时间描述是文本字段，直接返回
        return $value === '' ? null : $value;
    }

}
